package cn.huang.controller;

import cn.huang.constant.MessageConstant;
import cn.huang.entity.PageResult;
import cn.huang.entity.QueryPageBean;
import cn.huang.entity.Result;
import cn.huang.pojo.User;
import cn.huang.service.UserService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
//import org.springframework.security.core.userdetails.User;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户操作
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Reference
    private UserService userService;

    @Autowired
    private BCryptPasswordEncoder BCrypt;

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        try {
            return userService.pqgeQuery(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //添加用户
    @RequestMapping("/add")
    public Result add(@RequestBody User user , Integer[] roleIds){
        try{
            String password = user.getPassword();
            if (password != null){
                //使用BCrypt将明文密码进行加密
                password = BCrypt.encode(password);
                user.setPassword(password);
            }
            userService.add(user,roleIds);
        }catch (Exception e){
            e.printStackTrace();
            //新增套餐失败
            return new Result(false,MessageConstant.ADD_USER_FAIL);
        }
        return new Result(true,MessageConstant.ADD_USER_SUCCESS);
    }

    //修改用户 并且 给他添加角色信息
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            User user = userService.findById(id);
            //查询角色信息成功
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,user);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    //根据 用户ID 查询 角色项所包含的多个 角色项的ID
    @RequestMapping("/findRoleIdByUserId")
    public Result findRoleIdByUserId(Integer id){
        try {
            List<Integer> roleIds = userService.findRoleIdByUserId(id);
            // 用户信息查询成功
            return new Result(true,MessageConstant.QUERY_USER_SUCCESS,roleIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_USER_FAIL);
        }
    }

    //编辑用户管理
    @RequestMapping("/edit")
    public Result edit(@RequestBody User user , Integer[] roleIds){
        try {
            userService.edit(user,roleIds);
        }catch (Exception e){
            e.printStackTrace();
            // 编辑用户失败
            return new Result(false,MessageConstant.EDIT_USER_FAIL);
        }
        //编辑用户成功
        return new Result(true,MessageConstant.EDIT_USER_SUCCESS);
    }

    //获得当前登录用户的用户名
    @RequestMapping("/getUsername")
    public Result getUsername(){
        //当 spring security 完成认证后,会将当前用户信息保存到框架提供的上下文对象
        org.springframework.security.core.userdetails.User user = (org.springframework.security.core.userdetails.User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        System.out.println(user);

        if (user != null){
            String username = user.getUsername();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,username);//获取当前用户名成功,并把数据返回前台
        }
        return new Result(false,MessageConstant.GET_USERNAME_FAIL);//获取当前用户名失败
    }

    //删除用户
    @RequestMapping("/delete_User_Id")
    public Result delete_User_Id(Integer userId){
        try {
            userService.delete_User_Id(userId);
        }catch (Exception e){
            //删除角色失败
            return new Result(false,MessageConstant.DELETE_USER_FAIL);
        }
        //删除角色成功
        return new Result(true,MessageConstant.DELETE_USER_SUCCESS);
    }
}
